package cn.enilu.flash.api.controller.shop;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.dto.LabelListDTO;
import cn.enilu.flash.bean.entity.shop.LabelGoods;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.service.shop.LabelGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop/labelGoods")
public class LabelGoodsController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private LabelGoodsService labelGoodsService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Object list(){
        return Rets.success(labelGoodsService.queryAll());
    }

    @RequestMapping(method = RequestMethod.POST)
    @BussinessLog(value = "新增或修改标签", key = "name")
    public Object save(@RequestBody LabelGoods labelGoods){
        if(null == labelGoods.getId()){
            labelGoodsService.insert(labelGoods);
        }else {
            labelGoodsService.update(labelGoods);
        }
        return Rets.success();
    }

    @RequestMapping(value = "/saveList",method = RequestMethod.POST)
    @BussinessLog(value = "批量新增标签", key = "name")
    public Object saveList(@RequestBody LabelListDTO labelListDTO){
        if(null == labelListDTO.getGoodsId()){
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        labelListDTO.getLabelId().forEach(l->{
            LabelGoods labelGoods = new LabelGoods();
            labelGoods.setGoodsId(labelListDTO.getGoodsId());
            labelGoods.setLabelId(l);
            labelGoodsService.insert(labelGoods);
        });
        return Rets.success();
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @BussinessLog(value = "删除商品关联", key = "id")
    public Object remove(@RequestParam("id") Long id){
        if (id == null) {
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        labelGoodsService.deleteById(id);
        return Rets.success();
    }
}
